package me.seu.demo.tbox;

import java.util.ArrayList;
import java.util.List;

/**
 * 分割Hex数据
 *
 * @author: liangfeihu
 * @since: 2025-11-14 10:39
 */
public class HexDataSplitter {

    public static List<String> splitGroups(String hexData) {
        // 清理字符串：移除所有非十六进制字符（0-9, a-f, A-F）
        StringBuilder cleaned = new StringBuilder();
        for (char c : hexData.toCharArray()) {
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
                cleaned.append(c);
            }
        }
        String s = cleaned.toString();

        // 如果清理后字符串为空，返回空列表
        if (s.isEmpty()) {
            return new ArrayList<>();
        }

        // 查找所有"0001"的起始索引
        List<Integer> indices = new ArrayList<>();
        int pos = 0;
        while (pos < s.length()) {
            int idx = s.indexOf("0001", pos);
            if (idx == -1) {
                break;
            }
            indices.add(idx);
            pos = idx + 4; // 移动到"0001"之后继续查找
        }

        // 如果没有找到任何"0001"，返回整个清理后的字符串（但根据需求，这种情况很少见）
        if (indices.isEmpty()) {
            List<String> result = new ArrayList<>();
            result.add(s);
            return result;
        }

        // 分组：每组从"0001"开始，到下一个"0001"开始前结束
        List<String> groups = new ArrayList<>();
        for (int i = 0; i < indices.size(); i++) {
            int startIdx = indices.get(i);
            int endIdx;
            if (i == indices.size() - 1) {
                endIdx = s.length();
            } else {
                endIdx = indices.get(i + 1);
            }
            groups.add(s.substring(startIdx, endIdx));
        }
        return groups;
    }

    // 测试用例（可选，实际使用时无需调用）
    public static void main(String[] args) {
        String input = "0001000068fff20600020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000ac58000000000000b0000000000000001000068fff20700020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000ac58000000000000b0000000000000001000068fff20800020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000ac58000000000000b0000000000000001000068fff20900020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000ac58000000000000b0000000000000001000068fff20a00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000ac58000000000000b0000000000000001000068fff20b00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b0000000000000001000068fff20c00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b0000000000000001000068fff20d000200000080000000041388000000000005000000000060\"00060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b0000000000000001000068fff20e00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b0000000000000001000068fff20f00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b000000000000ffff0000e78bd005";

        List<String> groups = splitGroups(input);
        for (int i = 0; i < groups.size(); i++) {
            System.out.println("Group " + (i + 1) + ": " + groups.get(i));
        }
    }
}
